package pishen;

import java.util.ArrayList;
import java.util.List;

import edu.uci.ics.jung.graph.Graph;

public class LTSimulator {
	private Graph<Node, Edge> graph;
	private List<Node> infectedList;
	private List<Node> newestActiveList;
	
	public LTSimulator(Graph<Node, Edge> graph, List<Node> sourceList){
		this.graph = graph;
		this.infectedList = sourceList;
	}
	
	public void startSimulate(){
		int step = 0; 
		int numOfNewActivated = infectedList.size();
		newestActiveList = new ArrayList<Node>();
		
		while(numOfNewActivated > 0){
			System.out.println("step=" + step + " node infected=" + infectedList.size());
			step++;
			for(Node node: graph.getVertices()){
				if(!infectedList.contains(node)){
					double linkWeight = 1 / (double)graph.inDegree(node);
					double nodeLoading = 0;
					for(Node neighbor: graph.getPredecessors(node)){ // **Directed Graph**
						if(infectedList.contains(neighbor)){
							nodeLoading += linkWeight;
						}
					}
					if(nodeLoading >= 0.3){
						newestActiveList.add(node);
					}
				}
			}
			for(Node node: newestActiveList){
				infectedList.add(node);
			}
			numOfNewActivated = newestActiveList.size();
			newestActiveList.clear();
		}
	}
	
	public List<Node> getInfectedList(){
		return infectedList;
	}
}
